package servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import dao.FindPasswordDao;
import dao.LeaveMessageDAO;
import dao.SensitiveWordDAO;
import entity.User;

/**
 * Servlet implementation class LeaveMessageServlet
 */
@WebServlet("/LeaveMessageServlet")
public class LeaveMessageServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LeaveMessageServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html; charset=UTF-8");
		response.setHeader("content-type", "text/html;charset=UTF-8");
	    response.setCharacterEncoding("UTF-8");
	    request.setCharacterEncoding("UTF-8");
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
        if(user==null)
        {
        	
        }
		int send_id = user.getUser_id();
		int accept_id = 0;
		String context = null;
		try {
			accept_id = Integer.parseInt(request.getParameter("accept_id"));
			context = request.getParameter("context");
		} catch (NumberFormatException e) {
			// 如果浏览器没有传递start值
		}
		if(context==null||context.isEmpty()||context.equals(""))
		{
			request.setAttribute("id", accept_id);
			request.getRequestDispatcher("Ta_Home").forward(request, response);
		}
		else
		{
		SensitiveWordDAO sl = SensitiveWordDAO.getInstance();
		context = sl.Sensitive(context);
		LeaveMessageDAO lm = LeaveMessageDAO.getInstance();
		lm.SendMessage(send_id, accept_id, context);
		String email = "你好用户"+lm.getName(accept_id)+",用户"+lm.getName(send_id)+"给你有留言,请及时查看";
		FindPasswordDao fp = FindPasswordDao.getInstance();
	    fp.sendEmailInterface(accept_id, "留言", email);
		request.setAttribute("id", accept_id);	
		request.getRequestDispatcher("Ta_Home").forward(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
